package com.weilan.ark3.utils;



import com.weilan.ark3.entity.SocDto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SocUtils {
    /*
    Z
    * class:用于处理SOC区间参数的
    * */
    public static SocDto query() {
        SocDto socDto = new SocDto();
        String sql = "SELECT * FROM intervalParameters";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            socDto.setId(rs.getInt("id"));
            socDto.setMin_soc(rs.getInt("min_soc"));
            socDto.setBcp_soc(rs.getInt("bcp_soc"));
            socDto.setDcm_soc(rs.getInt("dcm_soc"));
            socDto.setMax_soc(rs.getInt("max_soc"));
            statement.executeQuery();
            conn.close();
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        return socDto;
    }


    /*
    * 更新数据库的参数---min_soc
    * */
    public static void updateToMinSoc(int value) {
        String sql = "UPDATE intervalParameters SET min_soc = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 更新数据库的参数---bcp_soc
     * */
    public static void updateToBcpSoc(int value) {
        String sql = "UPDATE intervalParameters SET bcp_soc = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * 更新数据库的参数---dcm_soc
     * */
    public static void updateToDcmSoc(int value) {
        String sql = "UPDATE intervalParameters SET dcm_soc = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * 更新数据库的参数---max_soc
     * */
    public static void updateToMaxSoc(int value) {
        String sql = "UPDATE intervalParameters SET max_soc = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
